package com.cbtx.module_sp.ui.adapter;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.cbtx.module_sp.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.txcb.lib.base.utils.LogUtil;

import java.util.List;

/**
 * @author : Lambert
 * date   : 2020/10/27 3:53 PM
 */
public class ImageDetailAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public ImageDetailAdapter(  @Nullable List<String> data) {
        super(R.layout.item_goods_detail, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {

        ImageView mIvDetail = helper.getView(R.id.mIvDetail);

        LogUtil.d(item);

        Glide.with(mContext)
                .asBitmap()
                .load(item)
                .placeholder(R.drawable.ic_default_big)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int w = resource.getWidth();//图片原始宽度
                        int h = resource.getHeight();//图片原始高度

//                        // 取 drawable 的长宽
//                        int w = resource.getIntrinsicWidth();
//                        int h = resource.getIntrinsicHeight();

                        LogUtil.d("w:"+w+"   h:"+h);
//                        LogUtil.d("w:"+resource.get);
                        ViewGroup.LayoutParams layoutParams = mIvDetail.getLayoutParams();
                        if (w<= 1050){
                            layoutParams.height = h;
                            layoutParams.width = w;
                        }else{
                            layoutParams.width = 1050;
                            layoutParams.height = (1050* h) / w;
                        }
                        mIvDetail.setLayoutParams(layoutParams);

                        mIvDetail.setImageBitmap(resource);
                    }
                });


        /**
        Glide.with(mContext)
                .load(item)
                .error(R.drawable.ic_default_big)
                .placeholder(R.drawable.ic_default_big)
//                .listener(new RequestListener<String, Bitmap>() {
//                    @Override
//                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
//                        Log.d(TAG, "onException " + e.toString());
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
//                        int width = bitmap.getWidth();
//                        int height = bitmap.getHeight();
//                        Log.d(TAG, "width2 " + width); //400px
//                        Log.d(TAG, "height2 " + height); //400px
//                        return false;
//                    }
//                })

                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

                        // 取 drawable 的长宽
                        int w = resource.getIntrinsicWidth();
                        int h = resource.getIntrinsicHeight();

                        LogUtil.d("w:"+w+"   h:"+h);
                        ViewGroup.LayoutParams layoutParams = mIvDetail.getLayoutParams();
                        if (w<= 1000){
                            layoutParams.height = h;
                        }else{
                            layoutParams.width = 1000;
                            layoutParams.height = (1000* h) / h;
                        }
                        mIvDetail.setLayoutParams(layoutParams);
                        return false;
                    }
                })
                .into(mIvDetail);
         */

    }
}
